package com.innoking.docsys.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.innoking.docsys.user.dto.UpmsOrganization;
import com.innoking.docsys.user.module.UpmsOrganizationVO;
import com.innoking.docsys.user.service.impl.UpmsOrganizationServiceImpl;
import com.innoking.docsys.utils.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;


/**
 * 组织机构管理Controller
 * @author zry
 * 2019-09-10
 */
@Controller
@RequestMapping("/api/org")
public class OrgnizationManageController {

    private static final Logger logger = LoggerFactory.getLogger(OrgnizationManageController.class);

    @Autowired
    public UpmsOrganizationServiceImpl upmsOrganizationService;

    /**
     * 查询所有组织机构
     *
     * @return
     */
    @RequestMapping(value = "/findOrgList")
    @ResponseBody
    public ResultObjVO findOrgList(@RequestBody String data) {
        List<UpmsOrganizationVO> list = upmsOrganizationService.findOrgList();
        List<UpmsOrganizationVO> treelist = TreeUtil.OrgList(list);
        if (treelist.size() == 0) {
            return ResultUtil.error("未查询到组织机构信息");
        }
        return ResultUtil.success(treelist);
    }

    /**
     * 根据组织机构id查询组织机构信息
     *
     * @return
     */
    @RequestMapping(value = "/findOrgListByOrgId")
    @ResponseBody
    public ResultObjVO findOrgListByOrgId(@RequestBody String data) {
        UpmsOrganization upmsOrganization = new UpmsOrganization();
        OtherPojo ob = JsonToOtherUtil.rtOtherString(data);
        String s = ob.getObj().toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        String orgId = jsonObject.get("orgId").toString();
        if (StringUtils.isNotBlank(orgId)) {
            List<UpmsOrganization> list = upmsOrganizationService.findOrgListByOrgId(orgId);
            upmsOrganization = list.get(0);
            if (upmsOrganization == null) {
                return ResultUtil.error("未查询到组织机构信息");
            }
        } else {
            return ResultUtil.error("参数为空");
        }
        return ResultUtil.success(upmsOrganization);
    }

    /**
     * 根据组织机构Code查询组织机构信息
     *
     * @return
     */
    @RequestMapping(value = "/findOrgListByOrgCode")
    @ResponseBody
    public ResultObjVO findOrgListByOrgCode(@RequestBody String data) {
        UpmsOrganization upmsOrganization = new UpmsOrganization();
        OtherPojo ob = JsonToOtherUtil.rtOtherString(data);
        String s = ob.getObj().toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        String orgCode = jsonObject.get("orgCode").toString();
        if (StringUtils.isNotBlank(orgCode)) {
            List<UpmsOrganization> list = upmsOrganizationService.findOrgListByOrgCode(orgCode);
            upmsOrganization = list.get(0);
            if (upmsOrganization == null) {
                return ResultUtil.error("未查询到组织机构信息");
            }
        } else {
            return ResultUtil.error("参数为空");
        }
        return ResultUtil.success(upmsOrganization);
    }
}